<?php
namespace Guestbook\Controller;
use Common\Controller\AdminBase;
class GuestbookController extends AdminBase {
    private $Guestbook = NULL;
    private $Messageboard = NULL;
    protected function _initialize() {
        parent::_initialize();
        $this->Guestbook = D('Guestbook/Guestbook');
        $this->Messageboard = D('Guestbook/Messageboard');
    }
    public function index() {
        if (IS_POST) {
            //排序
            $id = I('post.id');
            $listorder = I('post.listorder');
            if (empty($id)) {
                $this->error('请选择需要排序的信息！');
            }
            foreach ($id as $k => $oid) {
                $this->Guestbook->where(array('id' => $oid))->save(array('listorder' => $listorder[$k]));
            }
            $this->success('排序更新成功！', U('Guestbook/index'));
            exit;
        }
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['zhiwei'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }
        $count = $this->Guestbook->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Guestbook->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array( "id" => "DESC"))->select();
        foreach ($data as &$val){
            switch ($val['zhuti']){
                case '1':
                    $val['zhuti']="高补文科";
                    break;
                case '2':
                    $val['zhuti']="高补理科";
                    break;
                case '3':
                    $val['zhuti']="艺考补习";
                    break;
                case '4':
                    $val['zhuti']="中补";
                    break;
                case '5':
                    $val['zhuti']="高三应届";
                    break;
                case '6':
                    $val['zhuti']="初三应届";
                    break;
            }
            if($val['updatetime']=='0'){
                $val['updatetime']="";
            }else{
                $val['updatetime']=date('Y-m-d H:i:s');
            }
        }
        //统计
        $done   = $this->Guestbook->where(array('is_back' => 1))->count();
        $undone = $this->Guestbook->where(array('is_back' => 0))->count();

        $this->assign('data', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('done', $done);
        $this->assign('undone', $undone);
        $this->display();
    }
    public function back() {
        if (IS_POST) {
            $post = $_POST;
            if ($this->Guestbook->back($post)) {
                $this->success('留言回复成功！', U('Guestbook/index'));
            } else {
                $error = $this->Guestbook->getError();
                $this->error($error ? $error : '留言回复失败！');
            }
        } else {
            $Id = I('get.id', 0, 'intval');
            if (empty($Id)) {
                $this->error('请选择需要回复的留言信息！');
            }
            $info = $this->Guestbook->getInfo($Id);
            switch ($info['zhuti']){
                case '1':
                    $info['zhuti']="高补文科";
                    break;
                case '2':
                    $info['zhuti']="高补理科";
                    break;
                case '3':
                    $info['zhuti']="艺考补习";
                    break;
                case '4':
                    $info['zhuti']="中补";
                    break;
                case '5':
                    $info['zhuti']="高三应届";
                    break;
                case '6':
                    $info['zhuti']="初三应届";
                    break;
            }
            if (empty($info)) {
                $this->error('该留言信息不存在！');
            }
            $this->assign('info', $info);
            $this->display();
        }
    }
    public function delete() {
        if (IS_POST) {
            $Id = I('post.id');
        } else {
            $Id = I('get.id', 0, 'intval');
        }
        if (empty($Id)) {
            $this->error('请选择需要删除的信息！');
        }
        if ($this->Guestbook->deleteMessage($Id)) {
            $this->success('删除成功！！');
        } else {
            $this->error('删除失败！！');
        }
    }
}
